package com.wch.config;

import com.wch.pojo.Configuration;
import com.wch.pojo.MappedStatement;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.InputStream;
import java.util.List;

public class XMLMapperBuilder {
    private Configuration configuration;
    public XMLMapperBuilder(Configuration configuration) {
        this.configuration=configuration;
    }

    /**
     * 解析各个Mapper中的id、resultType、paramterType、sql并封装到对象MappedStatement中
     * @param inputStream
     * @throws DocumentException
     */
    public void parse(InputStream inputStream) throws DocumentException {
        //使用dom4j读取输入流
        Document document = new SAXReader().read(inputStream);
        //获得根节点
        Element rootElement = document.getRootElement();
        //获取namespace
        String namespace = rootElement.attributeValue("namespace");
        //获取每个select标签
        List<Element> list = rootElement.selectNodes("//select");
        //遍历集合list获取到单个select并将配置文件中的属性以及标签内容解析出来，封装成MappedStatement的对象，将对象放入configuration中
        list.forEach(element -> {
            String id = element.attributeValue("id");
            String resultType = element.attributeValue("resultType");
            String paramterType = element.attributeValue("paramterType");
            String sql = element.getTextTrim();
            MappedStatement mappedStatement = new MappedStatement(id,resultType,paramterType,sql);

            //key参数为namespace+id
            String key = namespace+"."+id;
            configuration.getMappedStatementMap().put(key, mappedStatement);
        });
        List<Element> list1 = rootElement.selectNodes("//insert");
        //遍历集合list获取到单个select并将配置文件中的属性以及标签内容解析出来，封装成MappedStatement的对象，将对象放入configuration中
        list1.forEach(element -> {
            String id = element.attributeValue("id");
            String resultType = element.attributeValue("resultType");
            String paramterType = element.attributeValue("paramterType");
            String sql = element.getTextTrim();
            MappedStatement mappedStatement = new MappedStatement(id,resultType,paramterType,sql);

            //key参数为namespace+id
            String key = namespace+"."+id;
            configuration.getMappedStatementMap().put(key, mappedStatement);
        });
        List<Element> list2 = rootElement.selectNodes("//update");
        //遍历集合list获取到单个select并将配置文件中的属性以及标签内容解析出来，封装成MappedStatement的对象，将对象放入configuration中
        list2.forEach(element -> {
            String id = element.attributeValue("id");
            String resultType = element.attributeValue("resultType");
            String paramterType = element.attributeValue("paramterType");
            String sql = element.getTextTrim();
            MappedStatement mappedStatement = new MappedStatement(id,resultType,paramterType,sql);

            //key参数为namespace+id
            String key = namespace+"."+id;
            configuration.getMappedStatementMap().put(key, mappedStatement);
        });
        List<Element> list3 = rootElement.selectNodes("//delete");
        //遍历集合list获取到单个select并将配置文件中的属性以及标签内容解析出来，封装成MappedStatement的对象，将对象放入configuration中
        list3.forEach(element -> {
            String id = element.attributeValue("id");
            String resultType = element.attributeValue("resultType");
            String paramterType = element.attributeValue("paramterType");
            String sql = element.getTextTrim();
            MappedStatement mappedStatement = new MappedStatement(id,resultType,paramterType,sql);

            //key参数为namespace+id
            String key = namespace+"."+id;
            configuration.getMappedStatementMap().put(key, mappedStatement);
        });
    }
}
